import json
import matplotlib.pyplot as plt

# 1. 读取本地 JSON 文件
with open(r'D:\files\农业机器人任务规划研究\ros_workspace\果园路网\西园\wgs84.json', 'r', encoding='utf-8') as f:
    data = json.load(f)

# 2. 解析经纬度数据
start_lats = [edge['sLatitude'] for edge in data['edges']]
start_lons = [edge['sLongitude'] for edge in data['edges']]
end_lats = [edge['eLatitude'] for edge in data['edges']]
end_lons = [edge['eLongitude'] for edge in data['edges']]

# 3. 绘图
plt.figure(figsize=(10, 8))

for i in range(len(data['edges'])):
    # 绘制每一条线段
    plt.plot([start_lons[i], end_lons[i]], [start_lats[i], end_lats[i]], 'bo-')
    # 标注起点和终点
    plt.text(start_lons[i], start_lats[i], f'S{i}', fontsize=8, color='green')
    plt.text(end_lons[i], end_lats[i], f'E{i}', fontsize=8, color='red')

plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.title('Path Visualization from edges.json')
plt.grid(True)
plt.axis('equal')  # 保持经纬度比例
plt.show()
